package com.runfast.waimai.service.impl;

import com.runfast.common.service.BaseService;
import com.runfast.waimai.dao.mapper.RunfastBusinessMapper;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastActivityService;
import com.runfast.waimai.service.RunfastActivityTargetService;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastFullLessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RunfastBusinessServiceImpl extends BaseService<RunfastBusiness, Integer, RunfastBusinessExample> implements RunfastBusinessService {
    @Autowired
    private RunfastActivityTargetService runfastActivityTargetService;
    @Autowired
    private RunfastActivityService runfastActivityService;
    @Autowired
    private RunfastFullLessService runfastFullLessService;
    public  List<RunfastBusiness> selectBusinessByExampleWithPageable(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.selectBusinessByExampleWithPageable(map);
    }

    public List<RunfastBusiness> selectBusinessWithGoods(Map<Object,Object> map1){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.selectBusinessWithGoods(map1);
    }

    public Integer countByMap(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.countByMap(map);
    }
    public Integer countBusinessByExample(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.countBusinessByExample(map);
    }
    public List<RunfastBusiness> selectAcivityByExampleWithPageable(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.selectAcivityByExampleWithPageable(map);
    }
    public Integer countAcivityByExampleWithPageable(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.countAcivityByExampleWithPageable(map);
    }
    public Integer countAcivityByExample(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.countAcivityByExample(map);
    }
    public List<RunfastBusiness>  selectRankByMapWithPageable(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.selectRankByMapWithPageable(map);
    }
    public Integer countRankByMapWithPageable(Map<Object,Object> map){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.countRankByMapWithPageable(map);
    }
    /** 商家参入的活动信息*/
    public void setActivityToBusiness(RunfastBusiness business1){
        List<RunfastActivity> activityList=new ArrayList<RunfastActivity>();
        Map<Object,Object> map2=new HashMap<Object, Object>();
        map2.put("businessId",business1.getId());
        map2.put("agentIds",business1.getAgentid());
        List<RunfastActivityTarget> targetList=runfastActivityTargetService.selectTargetAndActivityByMap(map2);
        if(targetList.size()>0){
            for(RunfastActivityTarget target:targetList){
                RunfastActivity activity=runfastActivityService.selectByPrimaryKey(target.getActivityid());
                if(activity. getPtype()==1){
                    /**满减的时候，联合满减表查询出活动的满减信息*/
                    RunfastFullLessExample fullLessExample=new RunfastFullLessExample();
                    fullLessExample.createCriteria().andActivityIdEqualTo(activity.getId());
                    List<RunfastFullLess> fullLessList=runfastFullLessService.selectByExample(fullLessExample);
                    activity.setRunfastFullLess(fullLessList);
                }
                activityList.add(activity);
            }
        }
        business1.setActivityList(activityList);
    }

    public  Map<String,String> getOnlineActivity(Integer agentId, String ptype, Integer activityId, Integer businessId){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.getOnlineActivity(agentId, ptype, activityId, businessId);
    }

    public List<RunfastBusiness> getAcivityBusinessByExampleWithPageable(Integer agentId, Integer activityId, Integer selectType, Integer businessId, String businessName,Pageable pageable){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.getAcivityBusinessByExampleWithPageable(agentId, activityId, selectType, businessId, businessName,pageable);
    }
    public Integer countAcivityBusinessByExampleWithPageable(Integer agentId, Integer activityId, Integer selectType, Integer businessId, String businessName,Pageable pageable){
        RunfastBusinessMapper runfastBusinessMapper=(RunfastBusinessMapper)this.mapper;
        return runfastBusinessMapper.countAcivityBusinessByExampleWithPageable(agentId, activityId, selectType, businessId, businessName,pageable);
    }
}